package com.uni.filter;

import com.uni.entity.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebFilter(filterName = "LoginFilter", value = {"/index.jsp"})
public class LoginFilter implements Filter {

    // 拦截未登陆用户访问首页
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain fchain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpSession session = req.getSession();

        User user = (User) session.getAttribute("userInfo");
        if(user == null) {
            ((HttpServletResponse) response).sendRedirect("code");
        }
        else
            fchain.doFilter(request, response);
    }
}
